<?php
namespace App\Http\Controllers;

use App\Http\Services\WishDefenderServices;
use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Model\Hint;

//擂主
class ApiWishDefenderController extends BaseController
{
    protected $wishDefenderService;

    public function __construct(){
        $this->wishDefenderService = new WishDefenderServices();
    }

    /**
     * 许愿池中奖记录列表 今日礼物榜单
     */
    public function winnerList(Request $request)
    {
        $limit = (int) $request->input('page_size', 10);
        $page = (int) $request->input('page_index', 0);
//        $luckId = (int) $request->get('luck_id', 0);
//        $userId = $this->getLoginUserId();

        $data = $this->wishDefenderService->winnerList($page, $limit);
        
        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 许愿池奖品列表
     */
    public function poolAwardListV2(Request $request)
    {
        $luckId = (int) $request->get('luck_id', 0);

        $data = $this->wishDefenderService->poolAwardListV2($luckId);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 抽奖
     */
    public function draw(Request $request){
        //获取抽奖次数
        $num = (int) $request->get('draw_num', 0);
        $luckId = (int) $request->get('luck_id', 0);
        //获取房间id
        $roomId = (int) $request->get('room_id', 0);

        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if($num < 1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if(empty($luckId)){
            Response::returnResult(Hint::ERR_NORMAL,[],'pool empty');
        }
        // if(!in_array($userId, [10000, 10001])) {
        //     Response::returnResult(Hint::PARAM_ERROR, [], '10:30-11:30系统维护，请稍后');
        // }

        //锁
        $key = 'DEFENDER:DRAW:'.$userId;
        $ifLock = redis_lock($key, 10);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, [], '抽奖中, 同一时间请勿重复请求');
        }

        $data = $this->wishDefenderService->draw($userId, $num, $roomId, $luckId);
       
        redis_unlock($key);

        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']); 
    }

    /**
     * 发飘屏
     */
    public function wishWheelDrawFull(Request $request){
        $orderId = (int) $request->input('order_id', 0);
        if(empty($orderId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('DEFENDER:wishPoolDrawFull:'.$orderId, 10);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishDefenderService->wishLuckDrawFull($orderId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 活动配置
     */
    public function config(){
        $rule = $this->wishDefenderService->getRule();
        $nowDrawInfo = $this->wishDefenderService->getProgess();
        $giftList = $this->wishDefenderService->poolAwardListV2($nowDrawInfo['luck_id']);

        $data = [
            'rule' => $rule['play_rule'],
            'luck_id' => $nowDrawInfo['luck_id'],
            'luck_name' => $nowDrawInfo['luck_name'],
            'winner_name' => $nowDrawInfo['winner_name'],
            'winner_avatar' => $nowDrawInfo['winner_avatar'],
            'gift_name' => $nowDrawInfo['gift_name'],
            'gift_img' => $nowDrawInfo['gift_img'],
            'gift_price' => $nowDrawInfo['gift_price'],
            'end_time' => $nowDrawInfo['end_time'],
            'end_minutes' => $nowDrawInfo['end_minutes'],
            'end_seconds' => $nowDrawInfo['end_seconds'],
            'start' => $nowDrawInfo['start'],
            'limit' => $nowDrawInfo['limit'],
            'gift_list' => $giftList,
            'price' => $this->wishDefenderService->getUniversePrice()
        ];
        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 兑换波波币
     */
    public function exchange(Request $request){
        $num = $request->post('num', 0);
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if($num < 1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $res = $this->wishDefenderService->exchange($num, $userId);

        Response::returnResult($res['code'],$res['data'],$res['msg']);
    }


    /**
     * 获取资产信息
     */
    public function balance(Request $request){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $res = $this->wishDefenderService->balance($userId);
        Response::returnResult(Hint::SUCCESS, $res);
    }

    /**
     * 获取用户中奖记录
     */
    public function wishLuckAwardListByUserId(Request $request){
        $pageNum = $request->get('page_index', 0);// 页数
        $pageSize = $request->get('page_size', 20);// 条数
        $userId = $this->getLoginUserId();
        $ua = $request->userAgent();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $data = $this->wishDefenderService->wishLuckAwardListByUserId($pageNum, $pageSize, $userId);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 获取单次抽奖价格
     */
    public function getPrice(){
        $luckId = 1000;
        $data = $this->wishDefenderService->getPrice($luckId);
        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 获取星辰变单次抽奖价格
     */
    public function getDefenderPrice(){
        $luckId = 1000;
        $data = $this->wishDefenderService->getPrice($luckId);
        Response::returnResult(Hint::SUCCESS, $data);
    }
}
